/**
 * @Title:  GroupPhotoActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2016-4-26 下午5:13:20 
 * @version:  V1.0 
 */

package artid.com.arttogoa.keeponrecord;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import artid.com.arttogoa.BaseActivity;
import artid.com.arttogoa.HomeActivity;
import artid.com.arttogoa.R;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.glide.GlideUtil;
import artid.com.arttogoa.parser.FilesBean;
import artid.com.arttogoa.parser.MicMapBean;
import artid.com.arttogoa.util.AlertUtils;
import artid.com.arttogoa.util.BitmapUtil;
import artid.com.arttogoa.util.CameraUtils;
import artid.com.arttogoa.util.MySharedPreferences;
import artid.com.arttogoa.util.VioceUtils;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author 熊彬
 * @data: 2016-4-26 下午5:13:20
 * @version: V1.0
 */
public class MicroscopicMapInfoActivity extends BaseActivity implements
		OnClickListener {
	private Button complete;
	private TextView title, right, prompt, text60;
	private RelativeLayout plus60, plus200;
	private AlertDialog alertDialog;
	private MicMapBean data;
	private EditText info;
	private ImageView micImg60, micImg200, vioce;
	private VioceUtils vu;
	private RelativeLayout back;
	private LinearLayout ll60, ll200;
	/** 上传模式 */
	private String updataType;
	/** 图片限制：0，无限制 1，只能选手机局部图 2，只能选200倍微观图 3,不能再选图 */
	private int pictureLimit = 0;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_microscopic_map);
		setContent(MicroscopicMapInfoActivity.this, getIntent());
		plus60 = (RelativeLayout) findViewById(R.id.rel_60);
		plus200 = (RelativeLayout) findViewById(R.id.rel_200);
		back = (RelativeLayout) findViewById(R.id.rela_back);
		ll60 = (LinearLayout) findViewById(R.id.ll_plus_60);
		ll200 = (LinearLayout) findViewById(R.id.ll_plus_200);
		back.setOnClickListener(this);
		text60 = (TextView) findViewById(R.id.tv_60);
		title = (TextView) findViewById(R.id.title_text);
		right = (TextView) findViewById(R.id.title_fb);
		prompt = (TextView) findViewById(R.id.tv_prompt);
		info = (EditText) findViewById(R.id.tv_info);
		micImg60 = (ImageView) findViewById(R.id.iv_mic_60);
		micImg200 = (ImageView) findViewById(R.id.iv_mic_200);
		right.setVisibility(View.VISIBLE);
		right.setText("完成");

		plus60.setOnClickListener(this);
		plus200.setOnClickListener(this);
		right.setOnClickListener(this);
		System.out.println(4);
		data = (MicMapBean) getIntent().getExtras().getSerializable("data");
		pictureLimit = (int) getIntent().getExtras().getInt("pictureLimit");
		System.out.println(5);
		vioce = (ImageView) findViewById(R.id.iv_vioce);
		vu = new VioceUtils(MicroscopicMapInfoActivity.this, vioce, info);
		vu.init();

		/** 初始化备案模式 **/
		updataType = MySharedPreferences.getMySharedPreferences(
				MicroscopicMapInfoActivity.this).getValue(Cache.ART_TYPE, "");
		/** 初始化标题标志 **/
		Drawable drawable = null;
		if (updataType.equals(ArtRecordActivity._PHONE)) {
			drawable = getResources().getDrawable(R.drawable.a_ico_iphone);
			title.setText("局部特征图");
			text60.setText("局部特征图");
			info.setHint("局部特征图及描述信息");
			prompt.setText("请添加第" + (data.getId() + 1) + "个点的局部特征图及描述信息");
			plus200.setVisibility(View.GONE);
		} else if (updataType.equals(ArtRecordActivity._FILING)) {
			drawable = getResources().getDrawable(R.drawable.a_ico_shebei);
			title.setText("微观特征点");
			info.setHint("微观特征点及描述信息");
			prompt.setText("请添加第" + (data.getId() + 1) + "个点的微观特征点及描述信息");
		}
		// / 这一步必须要做,否则不会显示.
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight());
		title.setCompoundDrawablePadding(20);
		title.setCompoundDrawables(drawable, null, null, null);
	}

	private boolean image60or200 = false;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.rela_back:
			finish();
			break;
		case R.id.rel_60:
			image60or200 = true;
			if (updataType.equals(ArtRecordActivity._FILING)) {
				Intent intent = new Intent(MicroscopicMapInfoActivity.this,
						HomeActivity.class);
				intent.putExtra("type", Cache.T_DIV);
				startActivityForResult(intent, 2);
			} else if (updataType.equals(ArtRecordActivity._PHONE)) {

				// alertDialog.dismiss();
				alertDialog = AlertUtils.initFilingPhotoAlertDialog(
						MicroscopicMapInfoActivity.this,
						MicroscopicMapInfoActivity.this.getWindowManager()
								.getDefaultDisplay().getWidth(),
						new OnClickListener() {

							@Override
							public void onClick(View view) {
								// TODO Auto-generated method stub
								switch (view.getId()) { 
								case R.id.tv_phone:
									CameraUtils
											.openCameraImage(MicroscopicMapInfoActivity.this);
									alertDialog.dismiss();
									break;
								case R.id.tv_choice:
									Intent intent = new Intent(
											MicroscopicMapInfoActivity.this,
											HomeActivity.class);
									intent.putExtra("type", Cache.T_PH);
									intent.putExtra("index", 1);
									intent.putExtra("pictureLimit",
											pictureLimit);
									startActivityForResult(intent, 2);
									alertDialog.dismiss();
									break;
								case R.id.tv_diver:
									Intent intent2 = new Intent(
											MicroscopicMapInfoActivity.this,
											HomeActivity.class);
									intent2.putExtra("type", Cache.T_PH);
									intent2.putExtra("index", 0);
									intent2.putExtra("pictureLimit",
											pictureLimit);
									startActivityForResult(intent2, 2);
									alertDialog.dismiss();
									break;
								default:
									break;
								}
							}
						}, pictureLimit);
			}
			break;
		case R.id.rel_200:
			image60or200 = false;
			Intent intent1 = new Intent(MicroscopicMapInfoActivity.this,
					HomeActivity.class);
			intent1.putExtra("type", Cache.T_DIV);
			startActivityForResult(intent1, 2);
			// alertDialog = AlertUtils.initPhotoAlertDialog(
			// MicroscopicMapInfoActivity.this,
			// MicroscopicMapInfoActivity.this.getWindowManager()
			// .getDefaultDisplay().getWidth(),
			// new OnClickListener() {
			//
			// @Override
			// public void onClick(View view) {
			// // TODO Auto-generated method stub
			// switch (view.getId()) {
			// case R.id.tv_phone:
			// CameraUtils
			// .openCameraImage(MicroscopicMapInfoActivity.this);
			// alertDialog.dismiss();
			// break;
			// case R.id.tv_choice:
			// Intent intent = new Intent(
			// MicroscopicMapInfoActivity.this,
			// HomeActivity.class);
			// startActivityForResult(intent, 2);
			// alertDialog.dismiss();
			// break;
			// default:
			// break;
			// }
			// }
			// });
			break;
		case R.id.title_fb:
			String infoString = info.getText().toString();
			if (TextUtils.isEmpty(imageUrl)) {
				Toast.makeText(MicroscopicMapInfoActivity.this, "请选择60倍图片~", 2)
						.show();
				return;
			} else if (updataType.equals(ArtRecordActivity._FILING)
					&& TextUtils.isEmpty(imageUrl200)) {
				Toast.makeText(MicroscopicMapInfoActivity.this, "请选择200倍图片~", 2)
						.show();
				return;
			} else if (TextUtils.isEmpty(infoString)) {
				Toast.makeText(MicroscopicMapInfoActivity.this, "请填写描述~", 2)
						.show();
				return;
			}
			data.setInfo(infoString);
			data.setArtMictype(micSD);
			data.setImage(imageUrl);
			data.setImage200(imageUrl200);
			Intent intent3 = new Intent(MicroscopicMapInfoActivity.this,
					MicroscopicMapSelectActivity.class);
			Bundle b = new Bundle();
			b.putSerializable("data", data);
			intent3.putExtras(b);
			MicroscopicMapInfoActivity.this.setResult(9, intent3);
			MicroscopicMapInfoActivity.this.finish();
			break;
		default:
			break;
		}
	}

	private String url, imageUrl, imageUrl200;
	private boolean micSD;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == CameraUtils.GET_IMAGE_BY_CAMERA) {
			final Uri uri = CameraUtils.imageUriFromCamera;
			url = BitmapUtil.getRealFilePath(MicroscopicMapInfoActivity.this,
					uri);
			micSD = false;
			setImage(url);
		} else if (requestCode == 2 && resultCode == 3) {
			Bundle bundle = data.getExtras();
			FilesBean fb = (FilesBean) bundle.getSerializable("image");
			// Bitmap bitmap = getLoacalBitmap(fb.getImgURL()); // 从本地取图片
			url = fb.getImgURL();
			micSD = true;
			setImage(url);
			// micImg.setImageBitmap(bitmap);
			// info.setText("图片名称：" + fb.getName() + "\n图片大小:" +
			// fb.getFileSize());
		} else if (requestCode == 2 && resultCode == 4) {
			Bundle bundle = data.getExtras();
			FilesBean fb = (FilesBean) bundle.getSerializable("image");
			System.out.println(fb.getThumbURL());
			// Bitmap bitmap = getLoacalBitmap(fb.getThumbURL()); // 从本地取图片
			url = fb.getThumbURL();
			micSD = false;
			setImage(url);
			// micImg.setImageBitmap(bitmap);
			// info.setText("图片名称：" + fb.getName() + "\n图片大小:" +
			// fb.getFileSize());
		}

	}

	private void setImage(String uri) {
		// TODO Auto-generated method stub

		if (image60or200) {
			GlideUtil.setImage1(MicroscopicMapInfoActivity.this, uri,
					R.drawable.yspba_img_mor, R.drawable.yspba_img_mor,
					micImg60);
			ll60.setVisibility(View.GONE);
			// Glide.with(MicroscopicMapInfoActivity.this).load(new File(uri))
			// .placeholder(R.drawable.yspba_img_mor)
			// .error(R.drawable.yspba_img_mor).into(micImg60);
			imageUrl = uri;
		} else {
			GlideUtil.setImage1(MicroscopicMapInfoActivity.this, uri,
					R.drawable.yspba_img_mor, R.drawable.yspba_img_mor,
					micImg200);
			ll200.setVisibility(View.GONE);
			// Glide.with(MicroscopicMapInfoActivity.this).load(new File(uri))
			// .placeholder(R.drawable.yspba_img_mor)
			// .error(R.drawable.yspba_img_mor).into(micImg200);
			imageUrl200 = uri;
		}
		url = "";
	}
	/**
	 * 加载本地图片 http://bbs.3gstdy.com
	 * 
	 * @param url
	 * @return
	 */
	// public Bitmap getLoacalBitmap(String url) {
	// imageUrl = url;
	// try {
	// FileInputStream fis = new FileInputStream(url);
	// Bitmap bitmap = BitmapFactory.decodeStream(fis);
	// return bitmap;
	// } catch (FileNotFoundException e) {
	// e.printStackTrace();
	// return null;
	// }
	// }
}
